import random
import math
from operator import itemgetter, attrgetter
from dataclasses import dataclass
import logging
from typing import List

logger = logging.getLogger(__name__)


@dataclass
class b_bcontainer:
    a: str
    b: int
    c: int


@dataclass
class k_motionmanager:
    """
    When initializing: set the init values from outside
    """

    b: bool
    c: int
    d: int
    e: int
    f: List
    g: List
    h: List
    i: float
    j: float
    k: float
    l: float
    m: float
    n: float
    o: float
    p: float
    q: float

    def a(self, lvar):
        m = max(abs(self.i - lvar.d), max(abs(self.j - lvar.e), abs(self.k - lvar.f)))
        self.i = lvar.d
        self.j = lvar.e
        self.k = lvar.f
        m2 = max(abs(self.l - lvar.a), max(abs(self.m - lvar.b), abs(self.n - lvar.c)))
        self.l = lvar.a
        self.m = lvar.b
        self.n = lvar.c
        m3 = max(abs(self.o - lvar.g), max(abs(self.p - lvar.h), abs(self.q - lvar.i)))
        self.o = lvar.g
        self.p = lvar.h
        self.q = lvar.i
        return max(m, max(m2, m3))

    def update(self, data):
        self.c += 1
        if self.c >= 300:
            return
        lvar = data
        if len(self.f) < 10:
            self.f.append(lvar)
            self.a(lvar)
        elif (lvar.j - self.e >= 1000 or lvar.k == 2) and len(self.h) < 10:
            self.h.append(lvar)
            self.e = lvar.j
            self.a(lvar)
        else:
            a2 = self.a(lvar)
            if a2 != 0.0:
                self.g.append({"a": lvar, "b": a2})
                if len(self.g) > 10:
                    self.g.sort(key=lambda x: x["a"].c)
                    self.g = self.g[:9]

    def b_b(self):
        arraylist = []
        for i in self.f:
            arraylist.append(i)
        for i in self.h:
            arraylist.append(i)
        for i in self.g:
            arraylist.append(i["a"])
        arraylist.sort(key=lambda x: x.c)
        st = ""
        j2 = int(0)
        j3 = int(self.d)
        j4 = int(j3)
        for lvar in arraylist:
            format = str(round(lvar.d, 2))
            format2 = str(round(lvar.e, 2))
            format3 = str(round(lvar.f, 2))
            format4 = str(round(lvar.a, 2))
            format5 = str(round(lvar.b, 2))
            format6 = str(round(lvar.c, 2))
            format7 = str(round(lvar.g, 2))
            format8 = str(round(lvar.h, 2))
            format9 = str(round(lvar.i, 2))
            j5 = lvar.j - j4
            if j5 < 0:
                j5 = 0
            r = (
                int(
                    float(format)
                    + float(format2)
                    + float(format3)
                    + float(format4)
                    + float(format5)
                    + float(format6)
                    + float(format7)
                    + float(format8)
                    + float(format9) * 100.0
                )
                + j5
            )
            str2 = "{},{},{},{},{},{},{},{},{},{},{};".format(
                j5,
                format,
                format2,
                format3,
                format4,
                format5,
                format6,
                format7,
                format8,
                format9,
                lvar.k,
            )
            logger.debug("Motion string: {}".format(str2))
            st += str2
            j2 += r
            j4 = lvar.j
        return b_bcontainer(a=st, b=j2, c=self.c)
